

<!-- 绑定手机号 -->
<template>
	<view class="container">
			
		
		<!-- 微信&手机 -->
		<view class="wxPhoneBind-Container" style="margin-top: 20rpx;">
			<image class="weixin" :src="icon.weixinIcon"></image>
			<image class="logo" :src="icon.logoIcon"></image>
	
			<image class="auth" :src="icon.authIcon"></image>
		</view>
		<view class="wxPhoneBind-desc">
			为了给您提供更好的服务，请您绑定手机号
		</view>
		
		
		<view class="oneRow">
			<view class="list-cell b-b radiusT10">
				<text class="cell-tit">手机号码</text>
				<input class="cell-tip" type="text" value="" v-model="phoneNumber" placeholder-class="inputHolder" placeholder="请输入手机号码"/>
			</view>
			<view class="list-cell b-b">
				<text class="cell-tit">验证码</text>
				<input class="cell-tip" type="text" value="" placeholder-class="inputHolder" v-model="code" placeholder="输入验证码"/>
				
				<verifyCode :phoneNumber="phoneNumber"></verifyCode>
			</view>
			<view class="list-cell radiusB10">
				<text class="cell-tit">设置密码</text>
				<input class="cell-tip" type="password" value="" v-model="password" placeholder-class="inputHolder" placeholder="请设置登录密码"/>
			</view>
			<!-- <view class="list-cell radiusB10">
				<text class="cell-tit">确认密码</text>
				<input class="cell-tip" type="password" value="" placeholder-class="inputHolder" placeholder="请确认新密码"/>
			</view> -->
		</view>
		
		
		<!-- 立即绑定 -->
		<view class="oneRow">
			<!-- #ifdef APP-PLUS -->
			<view class="list-cell save-btn" @click="bindPhoneHandler()">
				立即绑定
			</view>
			<!-- #endif -->
			
			<!-- #ifdef H5 -->
			<view class="list-cell save-btn" @click="bindPhoneHandler()">
				立即绑定
			</view>
			<!-- #endif -->
			
			<!-- #ifdef MP-WEIXIN -->
			<view class="list-cell save-btn">
				立即绑定
				<button open-type="getUserInfo" @getuserinfo="getUserInfo"></button>
			</view>
			<!-- #endif -->
		</view>
		<uni-popup ref="popup" type="dialog">
		  <uni-popup-dialog
			  type="success"
			  mode="base"
			  title=""
			  content="该手机号码已绑定其他微信，继续绑定，之前的微信需要重新注册才能使用，确定绑定吗？"
			  :before-close="true"
			  @close="close"
			  @confirm="confirm">
		  </uni-popup-dialog>
		</uni-popup>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	
	import verifyCode from '@/components/verify-code/verify-code'
	
	export default {
		computed: {
			...mapState(['hasLogin', 'jsApiAppId', 'url'])
		},
		components: {
			verifyCode
		},
		data() {
			return {
				icon: {
					backBtn: "/static/images/common/return.png",
					weixinIcon: "/static/images/login/bindPhone/weixin.png",
					logoIcon: "/static/images/login/bindPhone/logo.png",
					authIcon: "/static/images/login/bindPhone/auth.png"
				},
				
				fromLogin: null,
				bindFlag: false,
				
				phoneNumber:'',
				code:'',
				password:''
			};
		},
		onLoad(options) {
			this.fromLogin = options.fromLogin;
		},
		onShow() {
			
			var code = sessionStorage.getItem("wxCode");
			if(code) {
				sessionStorage.removeItem("wxCode");
				this.jsApiLoginCallback(code);
			}
		},
		onUnload() {
			if(this.fromLogin == 1) {
				if(!this.bindFlag) {
					this.bindBack();
				}
			}
		},
		methods: {
			
			...mapMutations(['login','bindBack']),
      close(done){
			  done()
      },
      confirm(done,value){
			  done()
      },
			// 绑定手机号
			bindPhoneHandler() {
        this.$refs.popup.open()
				var loginType = uni.getStorageSync("loginType");
				if("app" == loginType) {
					this.appBindPhoneHandler();
				}else if("h5" == loginType) {
					this.h5BindPhoneHandler();
				}
			},
			
			// App绑定手机号
			appBindPhoneHandler() {
				
				if(this.fromLogin == 1) {
				
					var wxInfo = JSON.parse(uni.getStorageSync("wxInfo"));
					
					var that = this;
					that.bindFlag = false;
					
					that.$api.globalUtil.requestAjax({
						url: '/CustomerApi/wxAppRegisterOrBindTelephoneForApi',
						method: "POST",
						data: {
							nickName: wxInfo.nickName,
							headImg: wxInfo.headImg,
							openId: wxInfo.openId,
							unionId: wxInfo.unionId,
							phone: that.phoneNumber,
							vCode: that.code,
							password:that.password
						},
						success: function(res) {
							if(res.data.success) {
								that.$api.msg('绑定成功');
								that.bindFlag = true;
								
								uni.setStorageSync('tokenInfo', JSON.stringify(res.data.response));
								that.login();
								
								setTimeout(function() {
									if(that.fromLogin == 1) {
										uni.switchTab({
											url: '/pages/index/index'
										});
									}else {
										uni.navigateBack({
											delta:1
										});
									}
								}, 1500);
							}
						},
						fail() {
							console.log("faillsdfds ");
						}
					});
				}else {
					this.appUpdateBindPhone();
				}
			},
			//App更换手机号
			appUpdateBindPhone() {
				
				var that = this;
				uni.login({
					provider: 'weixin',
					success: function (authRes) {
						
						// 获取用户信息
						uni.getUserInfo({
							provider: 'weixin',
							withCredentials: true,
							success: function (infoRes) {
								
								uni.setStorageSync("wxInfo", JSON.stringify({
									nickName: infoRes.userInfo.nickName,
									headImg: infoRes.userInfo.avatarUrl,
									openId: infoRes.userInfo.openId,
									unionId: infoRes.userInfo.unionId
								}));
								
								var wxInfo = JSON.parse(uni.getStorageSync("wxInfo"));
								that.bindFlag = false;

								that.$api.globalUtil.requestAjax({
									url: '/CustomerApi/wxAppRegisterOrBindTelephoneForApi',
									method: "POST",
									data: {
										nickName: wxInfo.nickName,
										headImg: wxInfo.headImg,
										openId: wxInfo.openId,
										unionId: wxInfo.unionId,
										phone: that.phoneNumber,
										vCode: that.code,
										password:that.password
									},
									success: function(res) {
										if(res.data.success) {
											that.$api.msg('绑定成功');
											that.bindFlag = true;
											
											uni.setStorageSync('tokenInfo', JSON.stringify(res.data.response));
											that.login();
											
											setTimeout(function() {
												if(that.fromLogin == 1) {
													uni.switchTab({
														url: '/pages/index/index'
													});
												}else {
													uni.navigateBack({
														delta:1
													});
												}
											}, 1500);
										}
									},
									fail() {
										console.log("faillsdfds ");
									}
								});
							},
							fail(err) {
								that.$api.msg("登录失败");
								uni.hideLoading();
							}
						});
					},
					fail(err) {
						that.$api.msg("登录失败");
						uni.hideLoading();
					}
				});
			},
			
			
			// H5绑定手机号
			h5BindPhoneHandler() {
				
				if(this.fromLogin == 1) {
				
					var wxInfo = JSON.parse(uni.getStorageSync("wxInfo"));
					
					var that = this;
					that.bindFlag = false;
					
					that.$api.globalUtil.requestAjax({
						url: '/CustomerApi/wxmpRegisterOrBindTelephoneForApi',
						method: "POST",
						data: {
							code: wxInfo.code,
							phone: that.phoneNumber,
							vCode: that.code,
							password:that.password
						},
						success: function(res) {
							if(res.data.success) {
								that.$api.msg('绑定成功');
								that.bindFlag = true;
								
								uni.setStorageSync('tokenInfo', JSON.stringify(res.data.response));
								that.login();
								
								setTimeout(function() {
									if(that.fromLogin == 1) {
										uni.switchTab({
											url: '/pages/index/index'
										});
									}else {
										uni.navigateBack({
											delta:1
										});
									}
								}, 1500);
							}
						},
						fail() {
							console.log("faillsdfds ");
						}
					});
				}else {
					this.h5UpdateBindPhone();
				}
			},
			//H5更换手机号
			h5UpdateBindPhone() {
				
				sessionStorage.setItem("backUrl", "/pagesUser/bindPhone");
				
				
				//获取code
				var APPID = this.jsApiAppId;
				var REDIRECT_URI = encodeURIComponent(this.url + "jsApiPay.html", "UTF-8");
				var getCodeUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+APPID+"&redirect_uri="+REDIRECT_URI+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
				window.location.replace(getCodeUrl);
			},
			jsApiLoginCallback(code) {
				
				var that = this;
				
				uni.setStorageSync("wxInfo", JSON.stringify({
					code: code
				}));
				
				var wxInfo = JSON.parse(uni.getStorageSync("wxInfo"));
				that.bindFlag = false;
				
				that.$api.globalUtil.requestAjax({
					url: '/CustomerApi/wxmpRegisterOrBindTelephoneForApi',
					method: "POST",
					data: {
						code: wxInfo.code,
						phone: that.phoneNumber,
						vCode: that.code,
						password:that.password
					},
					success: function(res) {
						if(res.data.success) {
							that.$api.msg('绑定成功');
							that.bindFlag = true;
							
							uni.setStorageSync('tokenInfo', JSON.stringify(res.data.response));
							that.login();
							
							setTimeout(function() {
								if(that.fromLogin == 1) {
									uni.switchTab({
										url: '/pages/index/index'
									});
								}else {
									uni.navigateBack({
										delta:1
									});
								}
							}, 1500);
						}
					},
					fail() {
						console.log("faillsdfds ");
					}
				});
			},
			
			
			//小程序绑定手机号
			wxmpBindPhoneHandler(e) {
				
				// var wxInfo = JSON.parse(uni.getStorageSync("wxInfo"));
				
				// var that = this;
				// that.bindFlag = false;
				
				// let that = this;
				// if (e.detail.errMsg == "getUserInfo:ok") {
					
				// 	uni.login({
				// 		provider: 'weixin',
				// 		success: function (res) {
							
				// 			var code = res.code;
				// 			uni.getUserInfo({
				// 				provider: 'weixin',
				// 				success: function (res) {
									
				// 					that.$api.globalUtil.requestAjax({
				// 						url: '/CustomerApi/wxmlRegisterOrBindTelephoneForApi',
				// 						method: "POST",
				// 						data: {
				// 							code: code,
				// 							rawData: res.rawData,
				// 							signature: res.signature,
				// 							encrypteData: res.encryptedData,
				// 							iv: res.iv
				// 						},
				// 						success: function(res) {
				// 							if(res.data.success) {
				// 								that.$api.msg('绑定成功');
				// 								that.bindFlag = true;
												
				// 								uni.setStorageSync('tokenInfo', JSON.stringify(res.data.response));
				// 								that.login();
												
				// 								setTimeout(function() {
				// 									if(that.fromLogin == 1) {
				// 										uni.switchTab({
				// 											url: '/pages/index/index'
				// 										});
				// 									}else {
				// 										uni.navigateBack({
				// 											delta:1
				// 										});
				// 									}
				// 								}, 1500);
				// 							}
				// 						},
				// 						fail() {
				// 							console.log("faillsdfds ");
				// 						}
				// 					});
				// 				}
				// 			});
				// 		},
				// 	});
				// }
			}
		}
	}
</script>

<style lang="scss">
	page{
		background: #F5F5F5;
	}
	
	/deep/ .uni-popup-dialog{
    padding-bottom: 30upx;
		.uni-dialog-content{
      padding: 5px 30px 15px 30px;
    }
    .uni-dialog-button-group{
      padding: 30upx 68upx;
      border: none;
      .uni-dialog-button{
        height: 100%;
        border: 1upx solid #FE5E49;
        border-radius: 35upx;
      }
      .uni-border-left{
        background: #FE5E49;
        margin-left: 44upx;
      }
      .uni-dialog-button-text{
        padding: 21upx 0upx;
        font-size: 30upx;
        font-family: PingFang SC;
        font-weight: 500;
        color: #FE5E49;
      }
      .uni-button-color{
        color: #FFFFFF;
      }
    }
	}
	
	// header部分
	.header-Container {
		position: relative;
		width: 100%;
		height: 176rpx;
		overflow: hidden;
		box-shadow: 0px 1px 1px 0px rgba(0, 0, 0, 0.08);
		background: rgba(255, 255, 255, 1);
	
		.leftBtns {
			display: flex;
			align-items: center;
			justify-content: flex-start;
			position: absolute;
			bottom: 0;
			width: 100%;
			height: 86rpx;
	
			.back {
				margin-left: 17rpx;
				width: 44rpx;
				height: 44rpx;
			}
		}
	
		.title {
			position: absolute;
			bottom: 0;
			width: 100%;
			height: 86rpx;
			line-height: 86rpx;
			text-align: center;
			font-size: 36rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: rgba(51, 51, 51, 1);
		}
	}
	
	.container {
		.oneRow {
			margin-top: 20rpx;
			padding: 0 30rpx;
			border-radius: 10rpx;
			
			.radiusT10 {
				border-radius: 10rpx 10rpx 0 0;
			}
			.radiusB10 {
				border-radius: 0 0 10rpx 10rpx;
			}
			
			.cell-tit {
				width: 120rpx;
				height: 100%;
				text-align: justify;
				
				&::after {
					display: inline-block;
					width: 100%;
					content: '';
					height: 0;
				}
			}
			
			.verify-Container {
				position: relative;
				
				&.borderOneRpx:after {
					border-color: #FE5E49;
					border-radius: 44rpx;
				}
				
				width:140rpx;
				height:44rpx;
				line-height: 44rpx;
				border-radius:22rpx;
				text-align: center;
				font-size:22rpx;
				font-family:PingFang;
				color:#FE5E49;
			}
		}
	}
	.list-cell{
		display:flex;
		align-items:center;
		padding: 0 40rpx 0 50rpx;
		height: 106rpx;
		line-height:106rpx;
		position:relative;
		background: #fff;
		// justify-content: flex-start;
		
		input {
			text-align: right;
			color: #333333;
		}
		
		.inputHolder {
			font-size:28rpx;
			font-family:PingFang;
			color:#BFBFBF;
			line-height:55px;
		}
		
		&.save-btn {
			justify-content: center;
			position: relative;
			margin: 0 auto;
			margin-top: 215rpx;
			width:600rpx;
			height:90rpx;
			line-height: 90rpx;
			background:#FE5E49;
			border-radius:45rpx;
			text-align: center;
			font-size:34rpx;
			font-family:PingFang;
			color:rgba(255,255,255,1);
			
			button {
				opacity: 0;
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 100%;
			}
		}
		&.cell-hover{
			background:#fafafa;
		}
		&.b-b:after{
			left: 30rpx;
			right: 30rpx;
		}
		&.m-t{
			margin-top: 16upx; 
		}
		.cell-more{
			align-self: baseline;
			font-size:$font-lg;
			color:$font-color-light;
			margin-left:10upx;
		}
		.cell-tit{
			margin-right:10upx;
			font-size:28rpx;
			font-family:PingFang;
			color:#666666;
		}
		.cell-tip{
			flex: 1;
			text-align: left;
			margin-left: 35rpx;
			font-size:28rpx;
			font-family:PingFang;
			color:#333333;
			// line-height:55rpx;
			
			&.unBind {
				color: #FE5E49;
			}
			&.unSet {
				color: #999999;
			}
		}
	}
	
	
	/* 绑定手机 */
	.wxPhoneBind-Container {
	    position: relative;
	    margin: 0 auto;
	    margin-top: 47rpx;
	    width: 354rpx;
	    height: 119rpx;
	}
	.wxPhoneBind-Container .weixin {
	    position: absolute;
	    left: 0;
	    top: 0;
	    width: 119rpx;
	    height: 119rpx;
	}
	.wxPhoneBind-Container .logo {
	    position: absolute;
	    right: 0;
	    top: 0;
	    width: 119rpx;
	    height: 119rpx;
	}
	.wxPhoneBind-Container .auth {
	    position: absolute;
	    top: 47rpx;
	    left: 50%;
	    margin-left: -17.5rpx;
	    width: 35rpx;
	    height: 28rpx;
	}
	.wxPhoneBind-desc {
	    margin-top: 34rpx;
		margin-bottom: 60rpx;
	    width: 100%;
	    height: 22rpx;
	    text-align: center;
	    font-size:22rpx;
	    font-family:PingFang SC;
	    color:rgba(102,102,102,1);
	}
</style>



